<?php
/**
 * @title 1334. 阈值距离内邻居最少的城市
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $n
     * @param Integer[][] $edges
     * @param Integer $distanceThreshold
     * @return Integer
     */
    function findTheCity($n, $edges, $distanceThreshold) {
        /**
         * @since 2023-11-14 点到点的最短路线的集合
         */
        $lineArray = [];
        for($i=0; $i<$n; $i++){
            $lineArray[$i] = [];
        }

        /**
         * @since 2023-11-14 遍历路线
         */
        foreach ($edges as list($from, $to, $weight)){
            /**
             * @since 2023-11-15 距离阈值范围内
             */
            if($weight <= $distanceThreshold){
                /**
                 * @since 2023-11-14 是否存在路线
                 */
                if(isset($lineArray[$from][$to])){
                    if($lineArray[$from][$to] > $weight){
                        $lineArray[$from][$to] = $weight;
                        $lineArray[$to][$from] = $weight;
                    } else {}
                } else {
                    /**
                     * @since 2023-11-14 不存在，直接赋值
                     */
                    $lineArray[$from][$to] = $weight;
                    $lineArray[$to][$from] = $weight;
                }
            } else {}
        }

        /**
         * @since 2023-11-14 在距离阈限制，每个点可以到达的点集合
         */
        for ($i=0; $i<$n; $i++){
            self::findMinLine($i, $lineArray, $distanceThreshold);
        }

        /**
         * @since 2023-11-15 距离阈值范围内，找最少的点
         */
        $k = 0;
        $minCnt = count($lineArray[0]);
        for($i=1; $i<$n; $i++){
            $cnt = count($lineArray[$i]);

            /**
             * @since 2023-11-15 更少的数量
             */
            if($cnt < $minCnt){
                $minCnt = $cnt;
                $k = $i;
            } elseif($cnt == $minCnt) {
                /**
                 * @since 2023-11-15 相同取更大值的键值，递增，无需判断
                 */
                $k = $i;
            } else {}
        }

        /**
         * @return
         */
        return $k;
    }

    /**
     * @title 返回距离阈值范围内，可以到达的点
     * @author start2004
     * @since 2023-11-15 Created
     *
     * @param $pointFirst
     * @param $lineArray
     * @param $distanceThreshold
     * @return array|mixed
     */
    function findMinLine($pointFirst = 0, &$lineArray = [], $distanceThreshold = 0){
        /**
         * @since 2023-11-15 找还可以通往的点
         */
        $nextPointArray = [];
        foreach ($lineArray[$pointFirst] as $pointSecond=>$weight2){
            foreach ($lineArray[$pointSecond] as $pointThird=>$weight3){
                $weight23 = $weight2 + $weight3;
                /**
                 * @since 2023-11-15 不回起点
                 * @since 2023-11-15 在距离阈值范围内
                 */
                if($pointThird != $pointFirst AND $weight23<=$distanceThreshold){
                    /**
                     * @since 2023-11-15 空，赋值
                     */
                    if(!isset($lineArray[$pointFirst][$pointThird])){
                        $lineArray[$pointFirst][$pointThird] = $weight23;
                        $nextPointArray[] = $weight23;
                    } elseif($lineArray[$pointFirst][$pointThird] > $weight23) {
                        /**
                         * @since 2023-11-15 更短的距离
                         */
                        $lineArray[$pointFirst][$pointThird] = $weight23;
                        $nextPointArray[] = $weight23;
                    } else {}
                } else {}
            }
        }

        /**
         * @since 2023-11-15 继续找的点，非空，继续
         */
        if($nextPointArray){
            self::findMinLine($pointFirst, $lineArray, $distanceThreshold);
        } else {}

        /**
         * @return
         */
        return true;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202311/2023.11.14.php
 */
$datas = [
    [4, [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], 4], // 3
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->findTheCity(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
